/**
 * Created by shc@lierda.com on 2017/2/22.
 */
var React = require('react');

var Icon = require('../components/Icon');
var ajax = require('../tools/ajax');

module.exports = React.createClass({
    getDefaultProps(){
        return {
            hact: '#', //页面返回路径
        }
    },
    getInitialState: function (){
        return {
            theme:"Default",
            title:"",//头部标题
            headr:'true',//头部右上角是否显示
            hact:'#',//页面返回路径
            wordBtn:"",//右上角文字
            Id:"",//收货地址Id
            picWord:"",//右上角文字+图片
            cartNum:0,//加入购物车商品数量
            bianjiFlag:1,//1为true 2为false
        }
    },
    componentDidMount: function () {
        setTimeout(this.attr,100);
        window.goBack = this.goBack;
        this.fleshNum();
    },
    fleshNum:function(){
       var cartNum= localStorage.getItem("cartNum");
        if(cartNum){
            this.setState({cartNum:cartNum});
        }
    },
    attr:function(){
        if(this.props.theme){
            this.setState({theme:this.props.theme});
        }
        if(this.props.title){
            this.setState({title:this.props.title});
        }
        if(this.props.headr){
            this.setState({headr:this.props.headr});
        }
        if(this.props.hact){
            this.setState({hact:this.state.hact});
        }
        if(this.props.wordBtn){
            this.setState({wordBtn:this.props.wordBtn});
        }
        if(this.props.Id) {
            this.setState({Id: this.props.Id})
        }
        if(this.props.picWord) {
            this.setState({picWord: this.props.picWord})
        }
    },
    //安卓返回键功能
    goBack(e){
        var back = localStorage.getItem("back");
        localStorage.back = "";
        if(back){
            location.href = back;
        }else{
            location.href = this.props.hact;
        }
        if(this.props.cartNum){
            this.props.cartNum();
        }
        var area1 = new LArea();
        area1.closeAll();
    },
    /**跳转页面*/
    action:function(e){
        var action = e.currentTarget.getAttribute("action");
        location.href = action;
    },
    cart(){
        var userId = localStorage.getItem("userId");
        localStorage.back = "";
        localStorage.searchPro = "";
        if(userId){
            location.href = "#cart";
        }else{
            location.href = "#login";
        }
    },
    /**通过收货地址ID删除地址*/
    deleteAddressById:function () {
        var uid = localStorage.getItem('userId');
        var AddressId = this.state.Id;
        if(AddressId){
            ajax("POST","deleteAddressById",{
                AddressId:AddressId,//id
                uid:uid
            },function (dt) {
                if(dt&&dt.success){
                    ajax("POST","getAddressByUid",{uid:uid},function(dt){
                        if(dt && dt.success){
                            var objs = dt.data;
                            if(objs[0]) {
                                ajax("POST", "changeAddress", {
                                    id: objs[0].id,
                                    uid: uid,//uid
                                    realname: objs[0].realname,//姓名realname
                                    mobile: objs[0].mobile,//手机号
                                    province: objs[0].province,//省
                                    city: objs[0].city,//市
                                    district: objs[0].district,//区/镇
                                    idnumber: objs[0].idnumber,//身份证
                                    address: objs[0].address,//详细地址
                                    zipcode: objs[0].zipcode,//邮政
                                    isdefault: 1
                                }, function (dt) {
                                    if (dt && dt.success) {
                                        location.href = "#addressInfo";
                                    }
                                });
                            }else{
                                location.href = "#addressInfo";
                            }
                        }
                    });
                }
            });
        }
    },
    bianji(){
        this.setState({bianjiFlag:2});
    },
    render: function () {
        var p=this;
        var cartNum = this.state.cartNum;
        return (
            <div className={p.state.theme+"_Header" + "  " + "head"}>
                <div className="head-l" onClick={p.goBack}>
                    <Icon icon="#icon-fanhui" style={{fontSize:"1.05rem"}}/>
                </div>
                <div className="h-cont">
                    <div className="u-head-logo">
                        <span className="xin_head-title">{p.state.title}</span>
                    </div>
                </div>
                <div className={p.state.headr=="true"? "head-f show":"head-f hidden"} onClick={p.cart}>
                    <span className="span-w">{cartNum?cartNum:0}</span>
                    <Icon icon="#icon-gouwuche" style={{fontSize:"1.05rem"}}/>
                </div>
                <div className="picWord" style={{display: p.state.picWord ? "block" : "none"}} onClick={p.action} action="#logisticsMap">
                    <Icon icon="#icon-wutizhuizong"/>
                    <p className="word">{p.state.picWord}</p>
                </div>
                <div className="wordBtn" style={{display: p.state.wordBtn ? "block" : "none"}}  onClick={p.state.wordBtn=="注册"?p.action:(p.state.wordBtn=="删除"?p.deleteAddressById:(p.state.wordBtn=="编辑"?p.bianji:""))}  action={p.props.wordBtn=="注册"?"#register":"#"}>
                    {p.state.wordBtn}
                </div>
            </div>
        )
    }
});
